package com.taskcollection.control;

import com.alibaba.fastjson.JSONObject;
import com.taskcollection.bean.ProjectBean;
import com.taskcollection.bean.ProjectProgressBean;
import com.taskcollection.bean.StudentBean;
import com.taskcollection.mapper.ProjectProgress;
import com.taskcollection.service.ProjectProgressService;
import com.taskcollection.service.ProjectService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by haocongshun on 2016/8/9.
 */
public class ProjectprogressControl {
    private ProjectProgressService pps;

    public ProjectProgressService getPps() {
        return pps;
    }

    public void setPps(ProjectProgressService pps) {
        this.pps = pps;
    }

    public void studentGetAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentBean stu = null;
        try {
            stu = (StudentBean)request.getSession().getAttribute("学生");
        }catch (Exception e){
            System.out.println("学生未登陆！");
        }
        if(stu==null){
            return;
        }
        JSONObject json = new JSONObject();
        String project_id = pps.getProject_id(stu.getStudent_id());
        List<ProjectProgressBean> lppb =  pps.getAll(project_id);
        ProjectService ps = new ProjectService();
        ProjectBean pb = ps.getById(project_id);
        json.put("getAll",lppb);
        json.put("project",pb);
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    public void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        JSONObject json = new JSONObject();
        json.put("msg",pps.del(id));
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        ProjectProgressService pps = new ProjectProgressService();
        ProjectProgressBean ppb = pps.getById(id);
        String  path = ppb.getProjectProgress_file();
        String realname = path.substring(path.indexOf("_")+1);
        path = request.getSession().getServletContext().getRealPath(path);
        File file = new File(path);
        if(!file.exists()){
            System.out.println("资源已被删除");
            return;
        }
        //设置响应头，控制浏览器下载该文件
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
         //读取要下载的文件，保存到文件输入流
        FileInputStream in = new FileInputStream(path);
         //创建输出流
         OutputStream out = response.getOutputStream();
         //创建缓冲区
        byte buffer[] = new byte[1024];
         int len = 0;
         //循环将输入流中的内容读取到缓冲区当中
         while((len=in.read(buffer))>0){
                //输出缓冲区的内容到浏览器，实现文件下载
                out.write(buffer, 0, len);
            }
         //关闭文件输入流
        in.close();
        //关闭输出流
        out.close();
    }
    public void teacherGetAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String project_id = request.getParameter("id");
        //ProjectProgressService pps = new ProjectProgressService();
        List<ProjectProgressBean> lppb =  pps.getAll(project_id);
        JSONObject json = new JSONObject();
        ProjectService ps = new ProjectService();
        ProjectBean pb = ps.getById(project_id);
        json.put("getAll",lppb);
        json.put("project",pb);
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
}
